﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.Data.SqlClient;
using System.IO;
using System.Data;
using System.Windows;
using System.Globalization;

namespace DexKey
{
    public class Utils
    {
        public static void EfetuarLogin(string nomeUsuario, string senha)
        {

            if (nomeUsuario.Equals(""))
            {
                MessageBox.Show("Informe seu usuário.");
            }
            else if (senha.Equals(""))
            {
                MessageBox.Show("Informe sua senha.");
            }
            else
            {
                string hashSenha = EncryptMd5(nomeUsuario, senha);
            }
        }

        public static string EncryptMd5LicenseKey(int _idTecnico, long _cnpj, string _modulos, DateTime _dataInicio, DateTime _dataFim)
        {
            MD5 md5Hasher = MD5.Create();
            byte[] valorCriptografado = md5Hasher.ComputeHash(Encoding.Default.GetBytes(_idTecnico + "_" + _cnpj + "_" + _modulos + "_" + _dataInicio + "_" + _dataFim));
            StringBuilder strBuilder = new StringBuilder();

            for (int i = 0; i < valorCriptografado.Length; i++)
            {
                strBuilder.Append(valorCriptografado[i].ToString("x2"));
            }

            return strBuilder.ToString().ToUpper();
        }

        public static string EncryptMd5(string _login, string _senha)
        {
            MD5 md5Hasher = MD5.Create();
            byte[] valorCriptografado = md5Hasher.ComputeHash(Encoding.Default.GetBytes(_login + "_" + _senha));
            StringBuilder strBuilder = new StringBuilder();

            for (int i = 0; i < valorCriptografado.Length; i++)
            {
                strBuilder.Append(valorCriptografado[i].ToString("x2"));
            }

            return strBuilder.ToString().ToUpper();
        }


        public static string EncryptMd5(string _senha, bool toUpper)
        {
            MD5 md5Hasher = MD5.Create();
            byte[] valorCriptografado = md5Hasher.ComputeHash(Encoding.Default.GetBytes(_senha));
            StringBuilder strBuilder = new StringBuilder();

            for (int i = 0; i < valorCriptografado.Length; i++)
            {
                strBuilder.Append(valorCriptografado[i].ToString("x2"));
            }

            if (toUpper)
            {
                return strBuilder.ToString().ToUpper();
            }
            else
            {
                return strBuilder.ToString();
            }
        }

        public static void Restart()
        {
            MessageBox.Show("A aplicação será reiniciada.");
            Application.Current.Shutdown();
            Kill();
        }

        public static void Kill()
        {
            System.Diagnostics.Process[] process = System.Diagnostics.Process.GetProcessesByName("DexGerencial");
            // Before starting the new process make sure no other MyProcessName is running.
            foreach (System.Diagnostics.Process p in process)
            {
                p.Kill();
            }


        }

        public static void ScripAtualizar(string caminho, string script, string ConectionString)
        {
            //Pega o caminho completo
            string fullPath = Directory.GetDirectoryRoot(caminho);

            //
            string arquivo = fullPath + "" + script;

            FileStream fileToRead = File.Open(arquivo, FileMode.Open);
            string linhas = "";
            string line;
            StreamReader sr = new StreamReader(fileToRead);

            while ((line = sr.ReadLine()) != null)
            {
                linhas += line + "\r\n";
            }

            SqlConnection SqlConnection = new SqlConnection(ConectionString);
            SqlCommand SqlCommand = new SqlCommand();
            SqlCommand.Connection = SqlConnection;
            try
            {
                SqlCommand.CommandText = linhas;

                SqlConnection.Open();
                SqlCommand.ExecuteNonQuery();
                MessageBox.Show("Banco de Dados Atualizado com sucesso");
            }
            catch (Exception)
            {
                MessageBox.Show("Ocorreu um erro ao executar o script favor entrar em contato com suporte !");
                throw;
            }

        }

        public static string RemoverCaracteres(string texto)
        {
            //texto = texto.ToUpper();
            string s = texto.Normalize(NormalizationForm.FormKD);
            StringBuilder sb = new StringBuilder();

            for (int k = 0; k < s.Length; k++)
            {
                UnicodeCategory uc = CharUnicodeInfo.GetUnicodeCategory(s[k]);
                if (uc == UnicodeCategory.DecimalDigitNumber)
                {
                    sb.Append(s[k]);
                }
            }
            return sb.ToString();
        }
    }
}

